package a;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class Paint extends JFrame implements ActionListener {
    private JPanel buttonPanel;
    private JButton lineButton, rectButton, ovalButton, textButton;
    private JComboBox<String> colorList, fontList;
    private JTextField textField;
    private JCheckBox filledBox;
    private PaintPanel paintPanel;

    public Paint() {
        super("Java画图软件");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(2, 4));
        lineButton = new JButton("直线");
        lineButton.addActionListener(this);
        rectButton = new JButton("矩形");
        rectButton.addActionListener(this);
        ovalButton = new JButton("椭圆");
        ovalButton.addActionListener(this);
        textButton = new JButton("文本");
        textButton.addActionListener(this);
        colorList = new JComboBox<String>(new String[] {"黑色", "红色", "绿色", "蓝色"});
        colorList.addActionListener(this);
        fontList = new JComboBox<String>(new String[] {"Arial", "Times New Roman", "Courier New"});
        fontList.addActionListener(this);
        textField = new JTextField("输入文本");
        filledBox = new JCheckBox("填充");
        buttonPanel.add(lineButton);
        buttonPanel.add(rectButton);
        buttonPanel.add(ovalButton);
        buttonPanel.add(textButton);
        buttonPanel.add(colorList);
        buttonPanel.add(fontList);
        buttonPanel.add(textField);
        buttonPanel.add(filledBox);
        contentPane.add(buttonPanel, BorderLayout.NORTH);
        paintPanel = new PaintPanel();
        contentPane.add(paintPanel, BorderLayout.CENTER);
        setSize(400, 300);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == lineButton) {
            paintPanel.setShape(PaintPanel.LINE);
        } else if (e.getSource() == rectButton) {
            paintPanel.setShape(PaintPanel.RECTANGLE);
        } else if (e.getSource() == ovalButton) {
            paintPanel.setShape(PaintPanel.OVAL);
        } else if (e.getSource() == textButton) {
            paintPanel.setShape(PaintPanel.TEXT);
        } else if (e.getSource() == colorList) {
            String colorName = (String)colorList.getSelectedItem();
            if (colorName.equals("黑色")) {
                paintPanel.setColor(Color.BLACK);
            } else if (colorName.equals("红色")) {
                paintPanel.setColor(Color.RED);
            } else if (colorName.equals("绿色")) {
                paintPanel.setColor(Color.GREEN);
            } else if (colorName.equals("蓝色")) {
                paintPanel.setColor(Color.BLUE);
            }
        } else if (e.getSource() == fontList) {
            String fontName = (String)fontList.getSelectedItem();
            if (fontName.equals("Arial")) {
                paintPanel.setFont(new Font("Arial", Font.PLAIN, 12));
            } else if (fontName.equals("Times New Roman")) {
                paintPanel.setFont(new Font("Times New Roman", Font.PLAIN, 12));
            } else if (fontName.equals("Courier New")) {
                paintPanel.setFont(new Font("Courier New", Font.PLAIN, 12));
            }
        }
        paintPanel.setFilled(filledBox.isSelected());
        paintPanel.setText(textField.getText());
    }

    public static void main(String[] args) {
        new Paint();
    }
}

class PaintPanel extends JPanel implements MouseListener, MouseMotionListener {
    public static final int LINE = 1;
    public static final int RECTANGLE = 2;
    public static final int OVAL = 3;
    public static final int TEXT = 4;
    private int shape = LINE;
    private Color color = Color.BLACK;
    private Font font = new Font("Arial", Font.PLAIN, 12);
    private boolean filled = false;
    private String text = "";
    private int startX, startY, endX, endY;

    public PaintPanel() {
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    public void setShape(int shape) {
        this.shape = shape;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public void setFont(Font font) {
        this.font = font;
    }

    public void setFilled(boolean filled) {
        this.filled = filled;
    }

    public void setText(String text) {
        this.text = text;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(color);
        switch (shape) {
            case LINE:
                g.drawLine(startX, startY, endX, endY);
                break;
            case RECTANGLE:
                if (filled) {
                    g.fillRect(startX, startY, endX - startX, endY - startY);
                } else {
                    g.drawRect(startX, startY, endX - startX, endY - startY);
                }
                break;
            case OVAL:
                if (filled) {
                    g.fillOval(startX, startY, endX - startX, endY - startY);
                } else {
                    g.drawOval(startX, startY, endX - startX, endY - startY);
                }
                break;
            case TEXT:
                g.setFont(font);
                g.drawString(text, startX, startY);
                break;
        }
    }

    public void mousePressed(MouseEvent e) {
        startX = e.getX();
        startY = e.getY();
    }

    public void mouseReleased(MouseEvent e) {
        endX = e.getX();
        endY = e.getY();
        repaint();
    }

    public void mouseDragged(MouseEvent e) {
        endX = e.getX();
        endY = e.getY();
        repaint();
    }

    public void mouseClicked(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mouseMoved(MouseEvent e) {}
}
